﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace Maxel
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();

            this.Load += new EventHandler(Form1_Load);
        }
        Dictionary<String, ComboBox> headerComboBox = new
  Dictionary<string, ComboBox>();
        private void Form1_Load(object sender, EventArgs e)
        {
            this.dataGridView1.ColumnHeadersHeightSizeMode =
DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.dataGridView1.ColumnHeadersHeight =
            this.dataGridView1.ColumnHeadersHeight * 2;
            DataGridViewTextBoxColumnWithSpecialColumnHeader col1 = new DataGridViewTextBoxColumnWithSpecialColumnHeader();
            this.dataGridView1.Columns.Add(col1);
            //handle the TextBoxValueChange event of the TextBox
            col1.HeaderCellEx.TextBoxValueChanged += new EventHandler(HeaderCellEx_TextBoxValueChanged);
            //handle the CheckBoxValueChange event of the CheckBox
            col1.HeaderCellEx.CheckBoxValueChanged += new EventHandler(HeaderCellEx_CheckBoxValueChanged);

            this.Paint += new PaintEventHandler(Form1_Paint);
        }

        void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();

            GraphicsPath p = new GraphicsPath();
            p.AddPolygon(new Point[] { new Point(50, 50), new Point(0, 100), new Point(100, 100) });

            g.FillPath(Brushes.Red, p);
        }

        void HeaderCellEx_CheckBoxValueChanged(object sender, EventArgs e)
        {
            MessageBox.Show("CheckBoxValueChanged");
        }

        void HeaderCellEx_TextBoxValueChanged(object sender, EventArgs e)
        {
            MessageBox.Show("TextBoxValueChanged");
        }
        // Click this button to write value to the TextBox and checkbox.
        private void btnGetHeaderCellTextBoxValue_Click(object sender, EventArgs e)
        {
            DataGridViewTextBoxColumnWithSpecialColumnHeader col = this.dataGridView1.Columns[0] as DataGridViewTextBoxColumnWithSpecialColumnHeader;
            col.HeaderCellEx.TextBoxValue = "23";
            col.HeaderCellEx.CheckBoxValue = CheckState.Checked;
        }
        // clear Value
        private void btnClear_Click(object sender, EventArgs e)
        {
            DataGridViewTextBoxColumnWithSpecialColumnHeader col = this.dataGridView1.Columns[0] as DataGridViewTextBoxColumnWithSpecialColumnHeader;
            col.HeaderCellEx.TextBoxValue = "";
            col.HeaderCellEx.CheckBoxValue = CheckState.Unchecked;
        }


    }


    public class TextBoxAndCheckBoxHeaderCell : DataGridViewColumnHeaderCell
    {
        enum SortType
        {
            ASC,
            DesC,
            None
        }


        public TextBox tb = new TextBox();
        public CheckBox cb = new CheckBox();
        public event EventHandler TextBoxValueChanged;//event fired when TextBoxValue property is changed.
        public event EventHandler CheckBoxValueChanged;//event fired when CheckBoxValue property is changed.

        SortType sType;


        public string TextBoxValue
        {
            get
            {
                return tb.Text;
            }
            set
            {
                if (value != tb.Text)
                {
                    tb.Text = value;
                    if (TextBoxValueChanged != null)
                    {
                        TextBoxValueChanged(this, EventArgs.Empty);
                    }
                }
            }
        }
        public CheckState CheckBoxValue
        {
            get
            {
                return cb.CheckState;
            }
            set
            {
                if (value != cb.CheckState)
                {
                    cb.CheckState = value;
                    if (CheckBoxValueChanged != null)
                    {
                        CheckBoxValueChanged(this, EventArgs.Empty);
                    }
                }
            }
        }
        protected override void OnClick(DataGridViewCellEventArgs e)
        {
            base.OnClick(e);
            if (sType == SortType.None)
            {
                sType = SortType.ASC;
            }
            else if (sType == SortType.ASC)
            {
                sType = SortType.DesC;
            }
            else
            {
                sType = SortType.ASC;
            }

        }
        public TextBoxAndCheckBoxHeaderCell()
        {
            tb.MouseClick += new MouseEventHandler(tb_MouseClick);
            this.cb.MouseClick += new MouseEventHandler(cb_MouseClick);
            this.tb.TextChanged += new EventHandler(tb_TextChanged);
            this.cb.CheckedChanged += new EventHandler(cb_CheckedChanged);
            sType = SortType.None;
        }

        void cb_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBoxValueChanged != null)
            {
                CheckBoxValueChanged(this, e);
            }
        }

        void tb_TextChanged(object sender, EventArgs e)
        {
            if (TextBoxValueChanged != null)
            {
                TextBoxValueChanged(this, e);
            }
        }

        void cb_MouseClick(object sender, MouseEventArgs e)
        {
            this.OnClick(new DataGridViewCellEventArgs(this.ColumnIndex, this.RowIndex));
        }

        void tb_MouseClick(object sender, MouseEventArgs e)
        {
            this.OnClick(new DataGridViewCellEventArgs(this.ColumnIndex, this.RowIndex));
        }

        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

            this.tb.Location = new Point(cellBounds.Left, cellBounds.Top);
            this.tb.Height = (cellBounds.Height - 2) / 2;
            this.tb.Width = cellBounds.Width - 2;
            this.cb.Location = new Point(cellBounds.Left, cellBounds.Height / 2);
            this.cb.Height = cellBounds.Height / 2;
            this.cb.Width = cellBounds.Width;

            // paint SortIcon
        }

    }
    public class DataGridViewTextBoxColumnWithSpecialColumnHeader : DataGridViewTextBoxColumn
    {
        TextBoxAndCheckBoxHeaderCell headerCell = new TextBoxAndCheckBoxHeaderCell();
        public DataGridViewTextBoxColumnWithSpecialColumnHeader()
        {
            this.CellTemplate = new DataGridViewTextBoxCell();
            this.HeaderCell = headerCell;
        }
        //when the column is just attached to the DataGridView, we add the controls to the dataGridView.
        protected override void OnDataGridViewChanged()
        {
            if (this.DataGridView != null)
            {
                this.DataGridView.Controls.Add(headerCell.tb);
                this.DataGridView.Controls.Add(headerCell.cb);
            }
        }
        public TextBoxAndCheckBoxHeaderCell HeaderCellEx
        {

            get
            {
                return this.headerCell;
            }
        }
    }
}
